using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using Facebook.Entity;
using Microsoft.WindowsMobile.PocketOutlook;

namespace DeviceFriendsSample
{
    public partial class FacebookFriends : Form
    {
        private OutlookSession session = new OutlookSession();

        public FacebookFriends()
        {
            InitializeComponent();
        }

        private void FacebookFriends_Load(object sender, EventArgs e)
        {
            //connect to facebook
            facebookService1.ApplicationKey = "8141812bae6d6912fa098a804f53389e";
            facebookService1.Secret = "3bcc261c1ffc6c549b7da1e1b79b9f46";
            facebookService1.ConnectToFacebook();

            RefreshFriendsList();
           
        }

        private void RefreshFriendsList()
        {
            lvFriends.Items.Clear();
            ilFriends.Images.Clear();

            Cursor.Current = Cursors.WaitCursor;
            lvFriends.BeginUpdate();

            Collection<User> friends = facebookService1.GetFriends();

            int i = 0;

            foreach (User u in friends)
            {
                ListViewItem lvi = new ListViewItem(u.Name);
                lvi.Tag = u;

                lvi.ImageIndex = i;
                ilFriends.Images.Add(u.PictureSmall);
                i++;

                lvFriends.Items.Add(lvi);
            }

            lvFriends.EndUpdate();

            Cursor.Current = Cursors.Default;
        }

        private void mnuView_Click(object sender, EventArgs e)
        {
            if (lvFriends.SelectedIndices.Count > 0 && lvFriends.SelectedIndices[0] > -1)
            {
                User u = (User)lvFriends.Items[lvFriends.SelectedIndices[0]].Tag;

                System.Diagnostics.Process.Start("http://m.facebook.com/profile.php?id=" + u.UserId, "");

            }
        }

        private void mnuSync_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvFriends.Items)
            {
                Contact c = null;

                User u = (User)lvi.Tag;

                //lookup the user in poom
                ContactCollection matches = session.Contacts.Items.Restrict("[FirstName] = \"" + u.FirstName + "\" AND [LastName] = \"" + u.LastName + "\"");
                if (matches != null && matches.Count > 0)
                {
                    c = matches[0];
                    
                }
                else
                {
                    //add a new contact
                    c = session.Contacts.Items.AddNew();
                    c.FirstName = u.FirstName;
                    c.LastName = u.LastName;

                    //mark this as a facebook contact (there won't be any contact information)
                    c.Categories = "Facebook";
                }

                //create temp filename
                string filename = System.IO.Path.Combine(System.IO.Path.GetTempPath(), u.UserId + ".jpg");
                //delete if it exists
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }

                //save and assign image
                u.PictureBig.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
                c.SetPicture(filename);

                //delete temp file (outlook stores a copy)
                System.IO.File.Delete(filename);

                //store userid for future reference
                c.Properties.Add("Facebook.UserId", typeof(string));
                c.Properties["Facebook.UserId"] = u.UserId;

                //save the contact
                c.Update();

            }
        }
    }
}